package com.epfresh.supplier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.global.BaseFragment;
import com.epfresh.api.global.DataManager;
import com.epfresh.api.global.MessageCentre;
import com.epfresh.api.helper.ApplicationHelper;
import com.epfresh.api.utils.CommonUtils;
import com.epfresh.supplier.R;
import com.epfresh.supplier.fragment.HomeNewFragment;
import com.epfresh.supplier.fragment.MineFragment;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.umeng.socialize.UMShareAPI;
import java.util.ArrayList;

public class HomeNewActivity extends BaseActivity {
    private ViewPager vpTop;
    private com.flyco.tablayout.CommonTabLayout tlBottom;
    private String[] mTitles = {"工作台", "我的"};
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private int[] mIconUnselectIds = {R.mipmap.tab_sale_unselect, R.mipmap.tab_mine_unselect};
    private int[] mIconSelectIds = {R.mipmap.tab_sale_select, R.mipmap.tab_mine_select};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private HomeNewFragment homeNewFragment = new HomeNewFragment();//工作台
    private BaseFragment mineFragment = new MineFragment();
    public boolean isActivityVisible;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (ApplicationHelper.getInstance().getUser().getAccess_token() == null) {
            onLogin();
        }
        BaseApplication.isHomeActivityAlive = true;
        CommonUtils.setImmersiveStatusBar(this);
        setContentView(R.layout.activity_home_new);
        //初始化view
        initView();
        //加载数据
        initData();
        ApplicationHelper.getInstance().isHomeActivityAlive = true;
        ApplicationHelper.getInstance().updateVersion(false, null);
        ApplicationHelper.getInstance().expiredTime();
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        DataManager.getInstance().isNoRefreshSell = false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        isActivityVisible = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        isActivityVisible = false;
        MessageCentre.getInstance().homeActivity = null;
    }

    @Override
    protected void onDestroy() {
        BaseApplication.isHomeActivityAlive = false;
        super.onDestroy();
    }

    private void initData() {
        mFragments.add(homeNewFragment);
        mFragments.add(mineFragment);
        vpTop.setAdapter(new HomeNewActivity.MyPagerAdapter(getSupportFragmentManager()));
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new com.epfresh.supplier.widget.TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        tlBottom.setTabData(mTabEntities);
        tlBottom.setOnTabSelectListener(new com.flyco.tablayout.listener.OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                vpTop.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        vpTop.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tlBottom.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected View getToolBar() {
        return null;
    }

    private void initView() {
        vpTop = findViewById(R.id.vp_top);
        tlBottom = findViewById(R.id.tl_bottom);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }

    protected int dp2px(float dp) {
        final float scale = getApplicationContext().getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }



    private long exitTime = 0; // 系统退出时间
    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - exitTime > 2000) {
            toast("再按一次返回键，退出程序");
            exitTime = System.currentTimeMillis();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //友盟qq分享需要
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
